//
//  YSMenuViewController.m
//  YSBookRead
//
//  Created by MOLBASE on 2018/11/14.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSMenuViewController.h"
#import "Masonry.h"
#import "YSMenuTopView.h"
#import "YSMenuBottomView.h"
#import "YSSettingView.h"

static const CGFloat YSTopViewH = 64;
static const CGFloat YSBottomViewH = 100;
static const CGFloat YSSettingViewH = 200;

@interface YSMenuViewController () <YSMenuTopViewDelegate, YSMenuBottomViewDelegate>

@property (nonatomic, weak) YSMenuTopView *topView;
@property (nonatomic, weak) YSMenuBottomView *bottomView;
@property (nonatomic, weak) YSSettingView *settingView;


@end

@implementation YSMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    // 允许获取电量信息
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

    [self setupUI];
}

- (void)setReadOperation:(YSReadOperation *)readOperation {
    _readOperation = readOperation;

    self.bottomView.slider.minimumValue = 0;
    self.bottomView.slider.maximumValue = readOperation.chapterContentList.count;
    self.bottomView.currentValue = readOperation.readRecord.readChapter;
}

- (void)setupUI {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHideEvent)]];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];

    YSMenuTopView *topView = [[YSMenuTopView alloc] init];
    topView.isLocal = self.isLocal;
    topView.backgroundColor = [UIColor colorWithRed:30/255.0 green:30/255.0 blue:30/255.0 alpha:1]; //1E1E1E
    [self.view addSubview:topView];
    topView.delegate = self;
    self.topView = topView;

    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(YSTopViewH);
                make.top.mas_equalTo(-YSTopViewH);
//        make.top.mas_equalTo(0);
    }];

    YSMenuBottomView *bottomView = [[YSMenuBottomView alloc] init];
    bottomView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85]; //1E1E1E
    [self.view addSubview:bottomView];
    bottomView.delegate = self;
    self.bottomView = bottomView;

    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(YSBottomViewH);
//        make.bottom.mas_equalTo(YSBottomViewH);
        make.bottom.mas_equalTo(0);
    }];

    YSSettingView *settingView = [[YSSettingView alloc] init];
    settingView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85]; //1E1E1E
    [self.view addSubview:settingView];
    self.settingView = settingView;

    [settingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(YSSettingViewH);
        make.bottom.mas_equalTo(YSSettingViewH);
    }];

    self.view.hidden = YES;
}

#pragma mark - Public Method
- (void)showMenuView {
    self.view.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
        }];
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
        }];
    } completion:^(BOOL finished) {
        if (finished) {
            [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
        }
    }];
}

- (void)showSettingView {
    [UIView animateWithDuration:0.25 animations:^{
        [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(-YSTopViewH);
        }];
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(YSBottomViewH);
        }];
        [self.settingView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
        }];
        [self.view layoutIfNeeded];
    }];
}

- (void)tapHideEvent {
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    [UIView animateWithDuration:0.25 animations:^{
        [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(-YSTopViewH);
        }];
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(YSBottomViewH);
        }];
        [self.settingView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(YSSettingViewH);
        }];
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
        }
        self.view.hidden = YES;
    }];
}

#pragma mark - YSMenuTopViewDelegate
- (void)menuTopView:(YSMenuTopView *)menuTopView didClickBackButton:(UIButton *)backButton {
    NSLog(@"返回");
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)menuTopView:(YSMenuTopView *)menuTopView didClickChangeSourceButton:(UIButton *)changeSourceButton {
     NSLog(@"换源");
    [self tapHideEvent];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuViewControllerDidClickChangeSourceButton:)]) {
        [self.delegate menuViewControllerDidClickChangeSourceButton:self];
    }
}

- (void)menuTopView:(YSMenuTopView *)menuTopView didClickReloadButton:(UIButton *)reloadButton {
    NSLog(@"刷新");
    [self tapHideEvent];

    if (self.delegate && [self.delegate respondsToSelector:@selector(menuViewControllerDidClickReloadButton:)]) {
        [self.delegate menuViewControllerDidClickReloadButton:self];
    }
}

#pragma mark - YSMenuBottomViewDelegate
- (void)menuBottomView:(YSMenuBottomView *)menuBottomView didClickDirectoryButton:(UIButton *)directoryButton {
    NSLog(@"目录");
    [self tapHideEvent];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuViewControllerDidClickDirectoryButton:)]) {
        [self.delegate menuViewControllerDidClickDirectoryButton:self];
    }
}

- (void)menuBottomView:(YSMenuBottomView *)menuBottomView didClickSettingButton:(UIButton *)settingButton {
    NSLog(@"设置");
    [self showSettingView];
}

- (void)menuBottomView:(YSMenuBottomView *)menuBottomView didClickDownloadButton:(UIButton *)downloadButton {
    NSLog(@"下载");
}

- (void)menuBottomView:(YSMenuBottomView *)menuBottomView didChangeChapter:(NSInteger)chapter {
    NSLog(@"更新章节");
    if (chapter == self.readOperation.readRecord.readChapter) {
        return;
    }
    [self.readOperation updateReadRecordWithChapter:chapter page:0];
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuViewControllerDidClickReloadButton:)]) {
        [self.delegate menuViewControllerDidClickReloadButton:self];
    }
}

@end
